package com.welink.cmp.meatdata.command;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.dao.metadata.FirewallDaoService;
import com.welink.cmp.dao.metadata.dataobject.FirewallDO;
import com.welink.cmp.dto.metadata.MetadataIdListQry;
import com.welink.cmp.dto.metadata.firewall.CreateFirewallAddCmd;
import com.welink.cmp.dto.metadata.firewall.clientobject.FirewallCO;
import com.welink.cmp.dto.metadata.firewall.query.FirewallListByPageQry;
import com.welink.cmp.dto.metadata.host.query.HostListByPageQry;
import com.welink.common.dto.PageResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * Firewall命令执行类
 * 负责处理Firewall相关的业务逻辑和数据操作
 */
@Component
public class FirewallCmdExe {

    @Autowired
    private FirewallDaoService firewallDaoService;

    /**
     * 保存Firewall信息
     *
     * @param addCmd 添加Firewall的命令对象
     * @return 新增Firewall的ID
     */
    public Long save(CreateFirewallAddCmd addCmd) {
        FirewallDO firewallDO = new FirewallDO();
        BeanUtils.copyProperties(addCmd, firewallDO);
        if (addCmd.isNeedsOperator()) {
            firewallDO.setCreateBy(addCmd.getOperator());
        }
        firewallDaoService.save(firewallDO);
        return firewallDO.getId();
    }

    /**
     * 配置Firewall基础信息
     *
     * @param firewallDO 配置Firewall基础信息的命令对象
     * @return 配置是否成功
     */
    public Boolean update(FirewallDO firewallDO) {
        return firewallDaoService.updateById(firewallDO);
    }


    /**
     * 根据ID查询Firewall信息
     *
     * @param id Firewall ID
     * @return Firewall业务对象
     */
    public FirewallCO queryById(Long id) {
        FirewallDO firewallDO = firewallDaoService.getById(id);
        FirewallCO firewallCO = new FirewallCO();
        BeanUtils.copyProperties(firewallDO, firewallCO);
        return firewallCO;
    }
    /**
     * 根据名称查询Firewall信息
     *
     * @param name Firewall Name
     * @return Firewall业务对象
     */
    public FirewallDO checkExistByName(String name) {
        LambdaQueryWrapper<FirewallDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FirewallDO::getName,name).eq(FirewallDO::getIsDel,0);
        final FirewallDO firewallDO = firewallDaoService.getOne(wrapper);
        return firewallDO;
    }

    /**
     * 列表查询Host信息
     *
     * @param listQry 列表查询条件
     * @return Host业务对象列表
     */
    public List<FirewallCO> listQuery(MetadataIdListQry listQry) {
        LambdaQueryWrapper<FirewallDO> wrapper = new LambdaQueryWrapper<>();
        // 根据列表查询条件设置查询条件
        wrapper.in(FirewallDO::getId,listQry.getIdList());
        List<FirewallDO> firewallDOS= firewallDaoService.list(wrapper);
        if (CollectionUtils.isEmpty(firewallDOS)){
            return Collections.EMPTY_LIST;
        }
        return GenericConvertor.toCOList(firewallDOS,FirewallCO.class);
    }

    /**
     * 分页查询Host信息
     *
     * @param pageQry 分页查询条件
     * @return 分页响应，包括Host列表和分页信息
     */
    public PageResponse<FirewallCO> pageQuery(FirewallListByPageQry pageQry) {
        final Page<FirewallDO> pageR = firewallDaoService.pageQry(pageQry);
        final List<FirewallDO> records = pageR.getRecords();
        final List<FirewallCO> coList = GenericConvertor.toCOList(records,FirewallCO.class);
        return PageResponse.of(coList,pageR.getTotal(),pageR.getSize(),pageR.getCurrent());
    }
}